﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CSVFormater
{
    class Formater
    {
        private string _code1 = "";
        private string _code2 = "";
        private string _code2ModelName = "";
        private string _code3 = "";
        private string _code4 = "";
        private string _photoFromPath;
        private string _photoToPath;

        const int imageFromIndex = 35;
        const int imageToIndex = 35;
        const int goodSpcificNameIndex = 0;
        const int SellingPriceIndex = 10;
        const int MarketPriceIndex = 12;
        const int PurchasePrinceIndex = 13;

        public Formater(string code1, string code2, string code2ModelName, string photoFromPath, string photoToPath)
        {
            this._code1 = code1;
            this._code2 = code2;
            this._code2ModelName = code2ModelName;
            this._photoFromPath = photoFromPath;
            this._photoToPath = photoToPath;

        }

        public Formater(string code1, string code2, string code2ModelName, string code3, string code4, string photoFromPath, string photoToPath)
        {
            this._code1 = code1;
            this._code2 = code2;
            this._code3 = code3;
            this._code4 = code4;
            this._code2ModelName = code2ModelName;
            this._photoFromPath = photoFromPath;
            this._photoToPath = photoToPath;

        }
 

        public  DataSet Format(DataSet dataSet)
        {
            #region load csv model
            string fileCsv = String.Format("Model\\" + _code2ModelName);
            var fi = new System.IO.FileInfo(fileCsv);
            // retrives directory
            string dirCsv = fi.DirectoryName.ToString();
            // retrives file name with extension
            string fileNevCsv = fi.Name.ToString();

            CSVLoader.WriteSchema(dirCsv, fileCsv);

            var dsTo = CSVLoader.LoadCsv(dirCsv, fileNevCsv);
            #endregion load csv model

            for (var tableIndex = 0; tableIndex < dataSet.Tables.Count; tableIndex ++)
            {
                var dtFrom = dataSet.Tables[tableIndex];

                var dtTo = dsTo.Tables[tableIndex];

                #region set default values
                for (int columnIndex = 0; columnIndex < dtTo.Columns.Count; columnIndex++)
                {

                    dtTo.Columns[columnIndex].DefaultValue = dtTo.Rows[0][columnIndex];
                }
                dtTo.Columns[6].DefaultValue = _code1;
                dtTo.Columns[7].DefaultValue = _code2;
                dtTo.Columns[8].DefaultValue = _code3;
                dtTo.Columns[9].DefaultValue = _code4;
                #endregion

                #region create same rows
                for (int rowIndex = 1; rowIndex < dtFrom.Rows.Count; rowIndex++)
                {
                    dtTo.Rows.Add(dtTo.NewRow());
                }
                #endregion

                #region copy setted column
                int[] columnFromIndex = { 39, 0, 7, 9 };
                int[] columnToIndex = { 0, 1, 10, 20 };
                
                /*copy datas*/
                for (int i = 0; i < columnToIndex.Count(); i++)
                {
                    for (int rowIndex = 0; rowIndex < dtFrom.Rows.Count; rowIndex++)
                    {

                        dtTo.Rows[rowIndex][columnToIndex[i]] = dtFrom.Rows[rowIndex][columnFromIndex[i]];
                    }
                }      
                for (int rowIndex = 0; rowIndex < dtFrom.Rows.Count; rowIndex++)
                {
                    /*copy image datas*/
                    var tempString = String.Format(dtFrom.Rows[rowIndex][imageFromIndex]+"");
                    var tempStrs = tempString.Split(':');
                    dtTo.Rows[rowIndex][imageToIndex] = String.Format(tempStrs[0]);
                    
                    /*format data*/
                    dtTo.Rows[rowIndex][goodSpcificNameIndex] = formatColumn(String.Format(dtTo.Rows[rowIndex][goodSpcificNameIndex] + ""));
                }
                #endregion copy setted column
            }
            return dsTo;
        }

        /*rename all the photos and rename all the photos Column */
        public void RenameAllPhotos(ref DataSet dsTo)
        {
            var dtTo = dsTo.Tables[0];
            for (int rowIndex = 0; rowIndex < dtTo.Rows.Count; rowIndex++)
            {

                /*rename all the photos */
                string fromExt = ".tbi";
                string toExt = ".jpg";
                string fromFolder = String.Format(_photoFromPath+"\\");
                string toFolder =String.Format(_photoToPath+"\\");
                string from = String.Format(fromFolder + dtTo.Rows[rowIndex][imageToIndex] + fromExt);
                string to = String.Format(toFolder + dtTo.Rows[rowIndex][goodSpcificNameIndex] + toExt);
                System.IO.File.Copy(from,to);

                /*rename all the photos Column */
                string replaceString =  String.Format(dtTo.Rows[rowIndex][goodSpcificNameIndex] + toExt);
                dtTo.Rows[rowIndex][imageToIndex] = replaceString;

            }
        }
        public string formatColumn(String columnMember)
        {
            string[] tempDates =  columnMember.Split(',');
            for(int i = 0; i < tempDates.Count() ; i++)
            {
                if(tempDates[i] != ",")
                {
                    return tempDates[i];
                }
            }
            return "";
        }
    }
}
